﻿using QntPlatform.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QntPlatform.Db
{
  public  class LogStore
    {
        static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
  .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=LogInfo.db")
  .UseAutoSyncStructure(true) //自动同步实体结构到数据库
  .Build(); //请务必定义成 Singleton 单例模式

        public static IFreeSql GetDb()
        {
            return fsql;
        }
       static Task task;
        public static void Insert(IEnumerable< LogInfo> infos)
        {
            if (task!=null)
            {
                task.Wait();
                task = null; 
            }
            task = GetDb().Insert<LogInfo>(infos).ExecuteAffrowsAsync();
        }
    }
    /// <summary>
    /// 交易对，订单号，方向、数量，交易价格，下单原因（购入、止损、止盈），主订单号，外部订单号
    /// </summary>
    public class LogInfo: EntityBaseLong
    {
        public long OwnerId { get; set; }
        public DateTime Time { get; set; }
        /// <summary>
        /// 交易，di，orderId，price	，amount，交易所，机器人信息,下单原因（购入、止损、止盈
        /// </summary>
        public string LogType { get; set; }
        public int LogLevel { get; set; }
        public static LogInfo CreateOrderLog(IExchange exchange, SideDirection sideDirection, decimal amount, decimal price, string actId, string pActId, string exteOrderId, LogInfo info = null)
        {
            if (info == null)
            {
                info = new LogInfo();
            }
            info.LogType = "OrderCreate";
            info.Info1 = $"{exchange.Name}_{exchange.Currency},{sideDirection},{amount},{price}";
            info.ActId = actId;
            info.ParentActId = pActId;
            info.ExteOrderId = exteOrderId;
            return info;
        }
        /// <summary>
        /// 交易对、方向、数量，交易价格
        /// </summary>
        public string Info1 { get; set; }
        public string Info2 { get; set; }
        public string Info3 { get; set; }
        public string Msg { get; set; }
        /// <summary>
        /// 动作内部id
        /// </summary>
        public string ActId { get; set; }
        /// <summary>
        /// 关联动作id
        /// </summary>
        public string ParentActId { get; set; }
        /// <summary>
        /// 外部订单号
        /// </summary>
        public string ExteOrderId { get; set; }


      
    }
}
